/**
 * SysWorkServiceImpl.java -- 2017-10-23 17:14:31 作者：欧增奇 -- 代码生成器自动生成 联系方式：425752797@qq.com http://www.ouzengqi.com/
 */
package com.ozq.sys.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ozq.common.page.Page;
import com.ozq.common.page.PageUtil;
import com.ozq.common.util.CommonUtils;
import com.ozq.sys.dao.entity.SysWork;
import com.ozq.sys.dao.mapper.SysWorkMapper;
import com.ozq.sys.service.inter.SysWorkService;

/**
 * <作品表接口实现> <功能详细描述>
 * 
 * @author 欧增奇
 * @version [V2.0, 2017-10-23 17:14:31]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Service("sysWorkService")
public class SysWorkServiceImpl implements SysWorkService
{
    static Logger logger = LoggerFactory.getLogger(SysWorkServiceImpl.class);

    /**
     * 作品表Mapper[SysWork]
     */
    @Autowired
    private SysWorkMapper sysWorkMapper;

    /** ------------------------------------公共-工具------------------------------------ **/
    /**
     * 分页公共实现
     */
    @Override
    public Page pageManage(Page page)
    {
        logger.debug("debug page: {}", page);

        // 默认每页10条记录
        if (page.getEveryPage() <= 0)
            page.setEveryPage(10);

        // 默认第一页
        if (page.getCurrentPage() <= 0)
            page.setCurrentPage(1);

        return PageUtil.createPage(page.getEveryPage(), page.getTotalCount(), page.getCurrentPage());
    }

    /**
     * 缓存清理
     */
    @Override
    public void clearCache()
    {
        logger.debug("清理作品表缓存成功:{}", "sysWork");
    }

    /** ------------------------------------公共-业务------------------------------------ **/
    /** --公共-业务-统计-- **/
    /**
     * 统计作品表所有的记录数
     * 
     * @return int
     */
    @Override
    public int countAll()
    {
        return sysWorkMapper.countAll();
    }

    /**
     * 按照条件精确统计作品表
     * 
     * @param sysWork
     * @return int
     */
    @Override
    public int countBy(SysWork sysWork)
    {
        logger.debug("debug sysWork: {}", sysWork);

        if (sysWork == null)
            return 0;

        return sysWorkMapper.countBy(sysWork);
    }

    /**
     * 按照条件模糊统计作品表
     * 
     * @param sysWork
     * @return int
     */
    @Override
    public int countLike(SysWork sysWork)
    {
        logger.debug("debug sysWork: {}", sysWork);

        if (sysWork == null)
            return 0;

        return sysWorkMapper.countLike(sysWork);
    }

    /** --公共-业务-添加-- **/
    /**
     * 插入作品表
     * 
     * @param sysWork
     * @return sysWork
     */
    @Override
    public SysWork add(SysWork sysWork)
    {
        logger.debug("debug sysWork: {}", sysWork);

        if (sysWork == null)
            return null;

        // 过滤数据
        sysWork.setWorkTitle(CommonUtils.filterScript(sysWork.getWorkTitle()));
        sysWork.setWorkSynopsis(CommonUtils.filterScript(sysWork.getWorkSynopsis()));

        sysWorkMapper.insert(sysWork);
        return sysWork;
    }

    /**
     * 插入作品表集合
     * 
     * @param sysWork
     * @return boolean
     */
    @Override
    public boolean addList(List<SysWork> sysWorks)
    {
        logger.debug("debug sysWorks: {}", sysWorks);

        if (sysWorks == null || sysWorks.isEmpty())
            return false;

        List<SysWork> inserts = filterEmpty(sysWorks);
        if (inserts.isEmpty())
            return false;

        sysWorkMapper.insertList(inserts);
        return true;
    }

    /**
     * 批量插入作品表，当id存在更新这条数据，不存在插入这条数据（注意：会影响所有字段）
     * 
     * @param sysWorks
     * @return boolean
     */
    @Override
    public boolean addUpdateList(List<SysWork> sysWorks)
    {
        logger.debug("debug sysWorks: {}", sysWorks);

        if (sysWorks == null || sysWorks.isEmpty())
            return false;

        List<SysWork> inserts = filterEmpty(sysWorks);
        if (inserts.isEmpty())
            return false;

        sysWorkMapper.insertUpdateList(inserts);
        return true;
    }

    /** --公共-业务-查询-- **/
    /**
     * 按照作品表查询,如果存在id，则用id查询(本方法只会返回一条数据，当查询有1条以上返回null)
     * 
     * @param sysWork
     * @return sysWork
     */
    @Override
    public SysWork find(SysWork sysWork)
    {
        logger.debug("debug sysWork: {}", sysWork);

        if (sysWork == null)
            return null;

        if (sysWork.getWorkId() != null)
            return sysWorkMapper.selectById(sysWork.getWorkId());

        sysWork.setStartLine(0);
        sysWork.setEndLine(1);
        List<SysWork> list = sysWorkMapper.listBy(sysWork);
        if (list == null || list.isEmpty())
            return null;

        return list.get(0);
    }

    /**
     * 按照id查询作品表
     * 
     * @param workId
     * @return sysWork
     */
    @Override
    public SysWork findById(Integer workId)
    {
        logger.debug("debug workId: {}", workId);

        if (workId == null)
            return null;

        return sysWorkMapper.selectById(workId);
    }

    /**
     * 按照ids查询作品表
     * 
     * @param workIds
     * @return sysWork集合
     */
    @Override
    public List<SysWork> findByIds(List<Integer> workIds)
    {
        logger.debug("debug workIds: {}", workIds);

        if (workIds == null || workIds.isEmpty())
            return null;

        return sysWorkMapper.selectByIds(workIds);
    }

    /**
     * 按照条件精确查询作品表返回集合方法
     * 
     * @param sysWork
     * @return sysWork
     */
    @Override
    public List<SysWork> listBy(SysWork sysWork)
    {
        logger.debug("debug sysWork: {}", sysWork);

        if (sysWork == null)
            return null;

        return sysWorkMapper.listBy(sysWork);
    }

    /**
     * 按照条件模糊查询作品表返回集合方法
     * 
     * @param sysWork
     * @return sysWork集合
     */
    @Override
    public List<SysWork> listLike(SysWork sysWork)
    {
        logger.debug("debug sysWork: {}", sysWork);

        if (sysWork == null)
            return null;

        return sysWorkMapper.listLike(sysWork);
    }

    /**
     * 按照分页条件精确查询作品表返回集合方法
     * 
     * @param sysWork
     * @return sysWork集合
     */
    @Override
    public List<SysWork> listPageBy(Page page, SysWork sysWork)
    {
        logger.debug("debug page: {}", page);
        logger.debug("debug sysWork: {}", sysWork);

        if (sysWork == null)
            sysWork = new SysWork();

        if (page != null)
        {
            sysWork.setStartLine(page.getBeginIndex());
            sysWork.setEndLine(page.getEveryPage());
        }

        if (sysWork.getOrderBy() == null)
            sysWork.setOrderBy(SysWork.FIELD_WORK_ADD_TIME + SysWork.ORDER_BY_DESC);

        return sysWorkMapper.listBy(sysWork);
    }

    /**
     * 按照分页条件模糊查询作品表返回集合方法
     * 
     * @param sysWork
     * @return sysWork集合
     */
    @Override
    public List<SysWork> listPageLike(Page page, SysWork sysWork)
    {
        logger.debug("debug page: {}", page);
        logger.debug("debug sysWork: {}", sysWork);

        if (sysWork == null)
            sysWork = new SysWork();

        if (page != null)
        {
            sysWork.setStartLine(page.getBeginIndex());
            sysWork.setEndLine(page.getEveryPage());
        }

        if (sysWork.getOrderBy() == null)
            sysWork.setOrderBy(SysWork.FIELD_WORK_ADD_TIME + SysWork.ORDER_BY_DESC);

        return sysWorkMapper.listLike(sysWork);
    }

    /** --公共-业务-修改-- **/
    /**
     * 修改作品表
     * 
     * @param sysWork
     * @return boolean
     */
    @Override
    public boolean update(SysWork sysWork)
    {
        logger.debug("debug sysWork: {}", sysWork);

        if (sysWork == null || sysWork.getWorkId() == null)
            return false;

        sysWorkMapper.update(sysWork);
        return true;
    }

    /**
     * 批量修改作品表
     * 
     * @param sysWorks
     * @return boolean
     */
    @Override
    public boolean updateList(List<SysWork> sysWorks)
    {
        logger.debug("debug sysWorks: {}", sysWorks);

        if (sysWorks == null)
            return false;

        List<SysWork> news = new ArrayList<SysWork>();
        for (SysWork sysWork : sysWorks)
            if (sysWork != null && sysWork.getWorkId() != null)
                news.add(sysWork);

        if (news.isEmpty())
            return false;

        sysWorkMapper.updateList(news);
        return true;
    }

    /** --公共-业务-删除-- **/
    /**
     * 指定id删除
     * 
     * @param workId
     * @return boolean
     */
    @Override
    public boolean deleteById(Integer workId)
    {
        logger.debug("debug workId: {}", workId);

        if (workId == null)
            return false;

        sysWorkMapper.deleteById(workId);
        return true;
    }

    /**
     * 批量删除指定ids
     * 
     * @param workId
     * @return boolean
     */
    @Override
    public boolean deleteByIds(List<Integer> workIds)
    {
        logger.debug("debug workIds: {}", workIds);

        if (workIds == null || workIds.isEmpty())
            return false;

        sysWorkMapper.deleteByIds(workIds);
        return true;
    }

    /**
     * 按照条件精确删除作品表
     * 
     * @param sysWork
     * @return boolean
     */
    @Override
    public boolean deleteBy(SysWork sysWork)
    {
        logger.debug("debug sysWork: {}", sysWork);

        if (sysWork == null)
            return false;

        sysWorkMapper.deleteBy(sysWork);
        return true;
    }

    /**
     * 按照条件模糊删除作品表
     * 
     * @param sysWork
     * @return boolean
     */
    @Override
    public boolean deleteLike(SysWork sysWork)
    {
        logger.debug("debug sysWork: {}", sysWork);

        if (sysWork == null)
            return false;

        sysWorkMapper.deleteLike(sysWork);
        return true;
    }

    /** --公共-业务-自定义-- **/
    /**
     * 自定义方法
     * 
     * @param sysWork
     * @return map集合
     */
    @Override
    public List<Map<String, Object>> custom(SysWork sysWork)
    {
        logger.debug("debug sysWork: {}", sysWork);

        // 自己实现自己的业务
        // sysWorkMapper.selectCustom(selectSQL);
        return null;
    }

    /** ------------------------------------私有-方法------------------------------------ **/
    /**
     * 过滤参数为空
     * 
     * @param sysWorks
     * @return sysWork集合
     */
    private List<SysWork> filterEmpty(List<SysWork> sysWorks)
    {
        List<SysWork> news = new ArrayList<SysWork>();
        for (SysWork sysWork : sysWorks)
            if (sysWork != null)
                news.add(sysWork);
        return news;
    }
}